from django.db import models
from django.contrib.auth.models import AbstractUser
from datetime import datetime
# Create your models here.


def user_avatar_path(instance, filename):
    """用户头像保存路径和文件名"""
    ext = filename.split('.')[-1]
    file_name = datetime.now().strftime("%Y%m%d_%H%M%S%f")
    date = datetime.now().strftime("%Y%m%d")
    file = file_name + '.' + ext
    return 'user/portrait/{date}/{file}'.format(date=date, file=file)

class User(AbstractUser):
    """
    用户表
    """
    nickname = models.CharField(max_length=50, blank=True, verbose_name='昵称')
    is_teacher = models.BooleanField(default=False, verbose_name='老师标识')  # 0为否，1为是老师
    birthday = models.DateField(verbose_name='生日', null=True, blank=True)
    gender = models.CharField(choices=(('male', '男'), ('female', '女')), null=True, blank=True, default='male', max_length=6, verbose_name='性别')
    address = models.CharField(max_length=100, default='', verbose_name='地址', blank=True, null=True)
    mobile = models.CharField(max_length=11, null=True, blank=True, verbose_name='手机')
    job = models.CharField(max_length=100, null=True, blank=True, verbose_name='职业')
    signature = models.CharField(max_length=300, null=True, blank=True, verbose_name='签名')
    intro = models.CharField(max_length=500, null=True, blank=True, verbose_name='简介')
    # nick_name = models.CharField(max_length=150, verbose_name="昵称", default="")
    # ImageField需要依赖pillow
    # user_avatar = models.ImageField(upload_to='user/%Y/%m', default='user/default.png', max_length=300, verbose_name='用户头像', blank=True)
    # 以后使用s3存储的设置
    user_avatar = models.ImageField(upload_to=user_avatar_path, default='user/default.png', max_length=300, verbose_name='用户头像', blank=True)
    # default_address = models.ForeignKey('Address', related_name='users', null=True, blank=True,
    #                                     on_delete=models.SET_NULL, verbose_name='默认地址')

    class Meta:
        db_table = "tb_users"
        verbose_name = '用户信息'
        verbose_name_plural = verbose_name
